package younian.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import younian.entity.Section;
import younian.entity.Zone;
import younian.service.TopicService;
import younian.service.ZoneService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
//刷新主页的列表  实时更新
@Component
public class DefaultAction extends ActionSupport implements ApplicationContextAware{

	private static final long serialVersionUID = 1L;
	
	
	private static ApplicationContext applicationContext;
	private Map<Section, Long> sectionTopicCount=new HashMap<Section, Long>();
	private Map<Section, Long> sectionGoodTopicCount=new HashMap<Section, Long>();
	
	
	private List<Zone> zoneList;
	public List<Zone> getZoneList() {
		return zoneList;
	}

	public void setZoneList(List<Zone> zoneList) {
		this.zoneList = zoneList;
	}

	public Map<Section, Long> getSectionTopicCount() {
		return sectionTopicCount;
	}

	public void setSectionTopicCount(Map<Section, Long> sectionTopicCount) {
		this.sectionTopicCount = sectionTopicCount;
	}

	public Map<Section, Long> getSectionGoodTopicCount() {
		return sectionGoodTopicCount;
	}

	public void setSectionGoodTopicCount(Map<Section, Long> sectionGoodTopicCount) {
		this.sectionGoodTopicCount = sectionGoodTopicCount;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext=applicationContext;
	}
	
	public String list()throws Exception {

		ZoneService zoneService=(ZoneService) applicationContext.getBean("zoneService");
		TopicService topicService=(TopicService) applicationContext.getBean("topicService");
		zoneList=zoneService.findZoneList(null, null);
		
		for (Zone zone : zoneList) {
			for (Section section : zone.getSectionList()) {
				Long tatolCount=topicService.getTopicCount(section);
				Long goodCount=topicService.getGoodTopicCount(section);
				
				sectionTopicCount.put(section, tatolCount);
				sectionGoodTopicCount.put(section, goodCount);
			}
		}
		return "success";
	}
}
